# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label

# pyglet  componets used by this scene
import pyglet
from pyglet import gl, font
from pyglet.window import key

import random

class MissionLayer(Layer):

    is_event_handler = True     #: enable pyglet's events

    def __init__( self, interface,area ):
        # call the parent's constructor
        super(MissionLayer, self).__init__()

        # keep track of the interface
        self.interface = interface
        
        # the planet on which this battle takes place
        self.area = area
        self.planet = area.planet
        
        # just a uniform color background
        self.add(ColorLayer(random.randint(0,255),40,40,200), z=-1)
        
        # add a label
        self.infoLabel = Label( "Mission Screen",
            font_size=12,
            multiline=True,
            height=100,
            width=360,
            x=10,
            y=SCREEN_HEIGHT - 20,
            anchor_x=font.Text.LEFT,
            anchor_y=font.Text.TOP )
        self.add( self.infoLabel )
        
        
    def on_enter(self):
        super( MissionLayer, self ).on_enter()   
        
        # shedule a function to update itself (the graphic) every frame
        self.schedule(self.update)      


    def on_exit(self):
        super( MissionLayer, self ).on_exit()   
        self.unschedule(self.update)   
        
    def update(self,dt, *args, **kwargs):
        infoText = "Mission Screen\n"
        infoText += "\n Mission on planet: " + self.planet.name
        infoText += "\n Area: " + str((self.area.x,self.area.y))
        infoText += "\n Minerals: " + str(self.area.minerals)
        infoText += "\n Organics: " + str(self.area.organics)
        infoText += "\n\n press ENTER to go back to planet screen "
        self.infoLabel.element.text = infoText
        


    def on_key_press( self, k , m ):
        #print "Code of pressed key: " + str(k)
        if k == key.ENTER:
            # end the current mission
            self.area.clear()
            self.unschedule(self.update)   
            director.pop() 

        if k == key.ESCAPE:
            self.unschedule(self.update)   
            director.pop() 
            # this should prevent ESC to pop this scene
            return pyglet.event.EVENT_HANDLED
            
               

# construct the scene here and return
# instead of doing this in the graphic interface (for example self.genericScene = Scene (GenericLayer()) )
# this is because here one can define more complicated scenes, independent of the interface
def MissionScene(interface,area):
    # in this generic example, build a scene composed by only one layer
    return Scene( MissionLayer(interface,area) )




